from typing import Optional
from datetime import datetime
from enum import Enum
from pydantic import BaseModel, Field, ConfigDict


class LoginType(str, Enum):
    PASSWORD = "password"
    SMS = "sms"
    WECHAT = "wechat"


class RegisterType(str, Enum):
    PASSWORD = "password"
    SMS = "sms"
    WECHAT = "wechat"


class UserStatus(str, Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    BANNED = "banned"


class UserRegister(BaseModel):
    register_type: RegisterType
    username: Optional[str] = None
    password: Optional[str] = None
    nickname: Optional[str] = None
    phone: Optional[str] = None
    email: Optional[str] = None
    sms_code: Optional[str] = None


class UserLogin(BaseModel):
    login_type: LoginType
    username: Optional[str] = None
    password: Optional[str] = None
    phone: Optional[str] = None
    sms_code: Optional[str] = None
    wechat_code: Optional[str] = None


class UserResponse(BaseModel):
    id: int
    username: Optional[str] = None
    nickname: Optional[str] = None
    avatar: Optional[str] = None
    background_cover: Optional[str] = None
    phone: Optional[str] = None
    email: Optional[str] = None
    gender: Optional[int] = None
    age: Optional[int] = None
    country: Optional[str] = None
    province: Optional[str] = None
    city: Optional[str] = None
    district: Optional[str] = None
    address: Optional[str] = None
    birthday: Optional[datetime] = None
    signature: Optional[str] = None
    description: Optional[str] = None
    xhs_id: Optional[str] = None
    dy_id: Optional[str] = None
    wb_id: Optional[str] = None
    wx_id: Optional[str] = None
    qq_id: Optional[str] = None
    points: Optional[int] = None
    level_id: Optional[int] = None
    level_name: Optional[str] = None
    level_icon: Optional[str] = None
    level_icon2: Optional[str] = None
    work_count: Optional[int] = None
    follow_count: Optional[int] = None
    fans_count: Optional[int] = None
    create_time: Optional[datetime] = None
    judge_competitions: Optional[list[dict]] = None

    model_config = ConfigDict(from_attributes=True)


class UserUpdate(BaseModel):
    nickname: Optional[str] = None
    avatar: Optional[str] = None
    background_cover: Optional[str] = None
    gender: Optional[int] = None
    age: Optional[int] = None
    country: Optional[str] = None
    province: Optional[str] = None
    city: Optional[str] = None
    district: Optional[str] = None
    address: Optional[str] = None
    birthday: Optional[datetime] = None
    signature: Optional[str] = None
    description: Optional[str] = None
    xhs_id: Optional[str] = None
    dy_id: Optional[str] = None
    wb_id: Optional[str] = None
    wx_id: Optional[str] = None
    qq_id: Optional[str] = None
    phone: Optional[str] = None
    email: Optional[str] = None


class TokenResponse(BaseModel):
    code: int
    access_token: str
    token_type: str
    user: UserResponse


class SMSCodeRequest(BaseModel):
    phone: str


class EmailCodeRequest(BaseModel):
    email: str


class WeChatLoginRequest(BaseModel):
    code: str


class BindAccountRequest(BaseModel):
    wx_id: Optional[str] = None
    qq_id: Optional[str] = None
    wb_id: Optional[str] = None
    email: Optional[str] = None
    email_code: Optional[str] = None


class UnbindAccountRequest(BaseModel):
    wx_id: Optional[bool] = None
    qq_id: Optional[bool] = None
    email: Optional[bool] = None


class UserMeResponse(BaseModel):
    code: int
    msg: str
    data: UserResponse


class LoginData(BaseModel):
    status: str
    access_token: str
    token_type: str
    user: UserResponse


class LoginResponseWrapper(BaseModel):
    code: int
    msg: str
    data: LoginData


class FollowListResponse(BaseModel):
    code: int
    msg: str
    data: list[UserResponse]
    total: int
    page: int
    page_size: int


class FollowRequest(BaseModel):
    user_id: int = Field(..., description="要关注/取消关注的用户ID")
    like: bool = Field(False, description="是否点赞（默认不点赞）")


class FollowResponse(BaseModel):
    code: int
    msg: str
    is_following: bool = Field(..., description="当前关注状态")
    follow_count: int = Field(..., description="用户关注数")
    fans_count: int = Field(..., description="被关注用户粉丝数")
    is_liked: bool = Field(..., description="当前点赞状态")
    like_count: int = Field(..., description="被点赞用户的点赞数")


class BackgroundCoverUpdate(BaseModel):
    background_cover: str = Field(..., description="背景封面URL")


class BackgroundCoverResponse(BaseModel):
    code: int
    msg: str
    data: Optional[dict] = None
